package com.tude._07vehicle.base.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入访问时间限定的拦截器...");
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        if (hour < 6 || hour > 23) {
            throw new RuntimeException("访问时间不在6:00-23:00之间，访问失败！");
            // 也可以通过重定向到一个页面来提示用户，然后return false
        }
        return true;
    }
}
